package com.chookss.tiku

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.chookss.tiku.entity.CanSignExam
import com.chookss.tiku.entity.LiveResult
import com.chookss.tiku.response.CanSignExamListResponse
import com.chookss.tiku.response.SignUpExamResponse
import com.chookss.tools.httpForm
import com.johnrambo.ktea.base.BaseViewModel

/**
 * Company:
 * Project:
 * Author: liuhaoge
 * Date: 2021/1/8 9:10
 * Note:
 */
class SignExamViewModel: BaseViewModel() {

    //下载考试列表
    fun getCanSignExams(): LiveData<LiveResult<List<CanSignExam>>> {
        var liveResult = MutableLiveData<LiveResult<List<CanSignExam>>>()
        jobs + httpForm<CanSignExamListResponse> {
            url = "api/exam/getCanSignExam"
            onSuccess = {
                liveResult.postValue(LiveResult(it.data))
            }
            onError = {
                liveResult.postValue(LiveResult(Error(it)))
            }
        }
        return liveResult
    }


    fun signUp(examSignCodes: String): LiveData<LiveResult<Any>> {
        var liveResult = MutableLiveData<LiveResult<Any>>()
        jobs + httpForm<SignUpExamResponse> {
            url = "api/exam/signUp"
            params = mutableMapOf("examSignCodes" to examSignCodes)
            onSuccess = {
                if (it.data?.rst == true) {
                    liveResult.postValue(LiveResult(null))
                } else {
                    liveResult.postValue(LiveResult(Error("申请出错")))
                }
            }
            onError = {
                liveResult.postValue(LiveResult(Error(it)))
            }
        }
        return liveResult
    }
}